﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.House.DAL
{
    /// <summary>
    /// 数据访问类House_MarkedInfo。
    /// </summary>
    public class House_MarkedInfo
    {
        public House_MarkedInfo()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid RecordId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from House_MarkedInfo where RecordId=@RecordId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.House.Entity.House_MarkedInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into House_MarkedInfo(");
            strSql.Append("RecordId,HouseId,HouseAddress,MarkCode,MarkDay,MarkStartTime,MarkEndTime,MarkPerson,MarkPersonID,MarkDepart,MarkDepartId,Status,CompanyId,CompanyName,AddTime,PropertyCode,PropertyPersonName)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@HouseId,@HouseAddress,@MarkCode,@MarkDay,@MarkStartTime,@MarkEndTime,@MarkPerson,@MarkPersonID,@MarkDepart,@MarkDepartId,@Status,@CompanyId,@CompanyName,@AddTime,@PropertyCode,@PropertyPersonName)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "HouseId", DbType.Guid, model.HouseId);
            db.AddInParameter(dbCommand, "HouseAddress", DbType.AnsiString, model.HouseAddress);
            db.AddInParameter(dbCommand, "MarkCode", DbType.AnsiString, model.MarkCode);
            db.AddInParameter(dbCommand, "MarkDay", DbType.DateTime, model.MarkDay);
            db.AddInParameter(dbCommand, "MarkStartTime", DbType.DateTime, model.MarkStartTime);
            db.AddInParameter(dbCommand, "MarkEndTime", DbType.DateTime, model.MarkEndTime);
            db.AddInParameter(dbCommand, "MarkPerson", DbType.AnsiString, model.MarkPerson);
            db.AddInParameter(dbCommand, "MarkPersonID", DbType.Guid, model.MarkPersonID);
            db.AddInParameter(dbCommand, "MarkDepart", DbType.AnsiString, model.MarkDepart);
            db.AddInParameter(dbCommand, "MarkDepartId", DbType.Guid, model.MarkDepartId);
            db.AddInParameter(dbCommand, "Status", DbType.Int32, model.Status);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "AddTime", DbType.DateTime, model.AddTime);
            db.AddInParameter(dbCommand, "PropertyCode", DbType.AnsiString, model.PropertyCode);
            db.AddInParameter(dbCommand, "PropertyPersonName", DbType.AnsiString, model.PropertyPersonName);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.House.Entity.House_MarkedInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update House_MarkedInfo set ");
            strSql.Append("HouseId=@HouseId,");
            strSql.Append("HouseAddress=@HouseAddress,");
            strSql.Append("MarkCode=@MarkCode,");
            strSql.Append("MarkDay=@MarkDay,");
            strSql.Append("MarkStartTime=@MarkStartTime,");
            strSql.Append("MarkEndTime=@MarkEndTime,");
            strSql.Append("MarkPerson=@MarkPerson,");
            strSql.Append("MarkPersonID=@MarkPersonID,");
            strSql.Append("MarkDepart=@MarkDepart,");
            strSql.Append("MarkDepartId=@MarkDepartId,");
            strSql.Append("Status=@Status,");
            strSql.Append("CompanyId=@CompanyId,");
            strSql.Append("CompanyName=@CompanyName,");
            strSql.Append("AddTime=@AddTime,");
            strSql.Append("PropertyCode=@PropertyCode,");
            strSql.Append("PropertyPersonName=@PropertyPersonName");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "HouseId", DbType.Guid, model.HouseId);
            db.AddInParameter(dbCommand, "HouseAddress", DbType.AnsiString, model.HouseAddress);
            db.AddInParameter(dbCommand, "MarkCode", DbType.AnsiString, model.MarkCode);
            db.AddInParameter(dbCommand, "MarkDay", DbType.DateTime, model.MarkDay);
            db.AddInParameter(dbCommand, "MarkStartTime", DbType.DateTime, model.MarkStartTime);
            db.AddInParameter(dbCommand, "MarkEndTime", DbType.DateTime, model.MarkEndTime);
            db.AddInParameter(dbCommand, "MarkPerson", DbType.AnsiString, model.MarkPerson);
            db.AddInParameter(dbCommand, "MarkPersonID", DbType.Guid, model.MarkPersonID);
            db.AddInParameter(dbCommand, "MarkDepart", DbType.AnsiString, model.MarkDepart);
            db.AddInParameter(dbCommand, "MarkDepartId", DbType.Guid, model.MarkDepartId);
            db.AddInParameter(dbCommand, "Status", DbType.Int32, model.Status);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "AddTime", DbType.DateTime, model.AddTime);
            db.AddInParameter(dbCommand, "PropertyCode", DbType.AnsiString, model.PropertyCode);
            db.AddInParameter(dbCommand, "PropertyPersonName", DbType.AnsiString, model.PropertyPersonName);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from House_MarkedInfo ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.House.Entity.House_MarkedInfo GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,HouseId,HouseAddress,MarkCode,MarkDay,MarkStartTime,MarkEndTime,MarkPerson,MarkPersonID,MarkDepart,MarkDepartId,Status,CompanyId,CompanyName,AddTime,PropertyCode,PropertyPersonName from House_MarkedInfo ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.House.Entity.House_MarkedInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.House.Entity.House_MarkedInfo GetModelByHouseId(Guid HouseId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 RecordId,HouseId,HouseAddress,MarkCode,MarkDay,MarkStartTime,MarkEndTime,MarkPerson,MarkPersonID,MarkDepart,MarkDepartId,Status,CompanyId,CompanyName,AddTime,PropertyCode,PropertyPersonName from House_MarkedInfo ");
            strSql.Append(" where HouseId=@HouseId ");
            strSql.Append(" order by MarkCode desc ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "HouseId", DbType.Guid, HouseId);
            Chenxin.Lib.House.Entity.House_MarkedInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,HouseId,HouseAddress,MarkCode,MarkDay,MarkStartTime,MarkEndTime,MarkPerson,MarkPersonID,MarkDepart,MarkDepartId,Status,CompanyId,CompanyName,AddTime,PropertyCode,PropertyPersonName ");
            strSql.Append(" FROM House_MarkedInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageIndex, int PageSize, String OrderField, string TableName, string StrWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, StrWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "House_MarkedInfo");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.House.Entity.House_MarkedInfo> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,HouseId,HouseAddress,MarkCode,MarkDay,MarkStartTime,MarkEndTime,MarkPerson,MarkPersonID,MarkDepart,MarkDepartId,Status,CompanyId,CompanyName,AddTime,PropertyCode,PropertyPersonName ");
            strSql.Append(" FROM House_MarkedInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.House.Entity.House_MarkedInfo> list = new List<Chenxin.Lib.House.Entity.House_MarkedInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.House.Entity.House_MarkedInfo ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.House.Entity.House_MarkedInfo model = new Chenxin.Lib.House.Entity.House_MarkedInfo();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["HouseId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.HouseId = new Guid(ojb.ToString());
            }
            model.HouseAddress = dataReader["HouseAddress"].ToString();
            model.MarkCode = dataReader["MarkCode"].ToString();
            ojb = dataReader["MarkDay"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.MarkDay = (DateTime)ojb;
            }
            ojb = dataReader["MarkStartTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.MarkStartTime = (DateTime)ojb;
            }
            ojb = dataReader["MarkEndTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.MarkEndTime = (DateTime)ojb;
            }
            model.MarkPerson = dataReader["MarkPerson"].ToString();
            ojb = dataReader["MarkPersonID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.MarkPersonID = new Guid(ojb.ToString());
            }
            model.MarkDepart = dataReader["MarkDepart"].ToString();
            ojb = dataReader["MarkDepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.MarkDepartId = new Guid(ojb.ToString());
            }
            ojb = dataReader["Status"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Status = (int)ojb;
            }
            ojb = dataReader["CompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CompanyId = new Guid(ojb.ToString());
            }
            model.CompanyName = dataReader["CompanyName"].ToString();
            ojb = dataReader["AddTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddTime = (DateTime)ojb;
            }
            model.PropertyCode = dataReader["PropertyCode"].ToString();
            model.PropertyPersonName = dataReader["PropertyPersonName"].ToString();
            return model;
        }

        #endregion  成员方法
    }
}

